/**
 * Created by sun on 2016/9/9.
 */

define(function (require) {
    var app = require('/../js/app');
    require('ui-table');

    app.useModule("ui.table");

    var datepicker = require('datepicker');
    var toastr =require('toastr');
    app.controller('userCtrl', ['$scope','$http','$filter' ,function ($scope, $http,$filter) {
        $scope.add={};
        $scope.list={};

        datepicker($scope);

        $scope.orderParams = {
            url: "/admin/sysuser/list",
            //ajax:{type: 1}
        };

        $scope.select2Options = {
            minimumResultsForSearch: Infinity //不出现搜索框
        };
        $scope.selectOptions = {
            allowClear: false,
            language : 'zh-CN',
        };

        //创建
        $scope.addOrderModal=function(){
            $scope.checkSubmitFlg = false;//自动匹配，当表单被提交过一次后checkSubmitFlg将变为true,根据判断将无法进行提交。
            $scope.addModal = !$scope.addModal ;
            $scope.add={};
        };

        //全局查询
        $scope.search = function() {
            $scope.ctrlScope.itemsearch.beginDateTime=$scope.ctrlScope.itemsearch.beginDateTimex;
            $scope.ctrlScope.itemsearch.endDateTime=$scope.ctrlScope.itemsearch.endDateTimex;
            $scope.ctrlScope.itemsearch.beginDateTime=$filter('date')($scope.ctrlScope.itemsearch.beginDateTime,'yyyy-MM-dd');
            $scope.ctrlScope.itemsearch.endDateTime=$filter('date')($scope.ctrlScope.itemsearch.endDateTime,'yyyy-MM-dd');
            //表格查询参数设置
            $scope.ctrlScope.itemsearch.type=$scope.ctrlScope.itemsearch.type;
            $scope.orderParams.ajaxParam=$scope.ctrlScope.itemsearch;
            //表格刷新
            $scope.orderParams.reload();
        };

        //全局查询重置
        $scope.reset=function(){
            $scope.orderParams.ajaxParam={};
            $scope.ctrlScope.itemsearch={};
            $scope.orderParams.reload();
        };

        //获取角色列表
        $scope.getRoleList= function () {
            $http({
                method: 'POST',
                url: "/admin/role/listNoPage",
            }).success(function(data) {
                $scope.roleList=data;
            }).error(function(data) {
            });
        };
        $scope.getRoleList()

        //创建
        $scope.addSubmit = function() {
            if ($scope.checkSubmitFlg == true) {
                return false; //当表单被提交过一次后checkSubmitFlg将变为true,根据判断将无法进行提交。
            }
            $scope.checkSubmitFlg = true;
            $scope.add.birthday=$filter('date')($scope.add.birthday,'yyyy-MM-dd');
            var parm=$scope.add;
            console.log(parm);
            $http({
                method: 'POST',
                url: "/admin/sysuser/add",
                data:parm,
            }).success(function(data) {

                if(data.result=="success"){
                    toastr.success("创建成功！");
                    $scope.addModal = !$scope.addModal;
                    $scope.orderParams.reload();
                }else{
                    toastr.error("创建失败");
                }

            }).error(function(data) {
            });
        };
        //编辑
        $scope.userModal = function(id){
            $scope.checkSubmitFlg = false;//自动匹配，当表单被提交过一次后checkSubmitFlg将变为true,根据判断将无法进行提交。
            $scope.detailsModal=!$scope.detailsModal;
            $scope.getOne(id);
        };
        $scope.getOne=function(id){
            $http({
                method: 'POST',
                url: "/admin/sysuser/getOne",
                data:{
                    id:id
                },
            }).success(function(data) {
                console.log(data)
                $scope.list=data.user;
                $scope.list.character=data.userRole.roleId;


            }).error(function(data) {
            });
        };
        //修改
        $scope.saveSubmit = function(){
            if ($scope.checkSubmitFlg == true) {
                return false; //当表单被提交过一次后checkSubmitFlg将变为true,根据判断将无法进行提交。
            }
            $scope.checkSubmitFlg = true;
            var parm=$scope.list;
            console.log(parm);
            parm.birthday=$filter('date')(parm.birthday,'yyyy-MM-dd');
            console.info(parm);
            $http({
                method: 'POST',
                url: "/admin/sysuser/modify",
                data:parm,
            }).success(function(data) {

                if(data.result=="success"){
                    toastr.success("保存成功！");
                    $scope.detailsModal = !$scope.detailsModal;
                    $scope.orderParams.reload();
                }else{
                    toastr.error("保存失败");
                }

            }).error(function(data) {
            });
        };
        //密码
        $scope.modify = function(id){
            $scope.modify.id=id;
            $scope.modify.newPassword='';
            $scope.modifyModal= !$scope.modifyModal;
        };
        //修改密码
        $scope.modifySubmit = function(){
            $http({
                method: 'POST',
                url: "/admin/sysuser/modifyPassword",
                data:{
                    id:$scope.modify.id,
                    password:$scope.modify.newPassword
                },
            }).success(function(data) {
                if(data.result=="success"){
                    toastr.success("修改成功！");
                    $scope.modifyModal = !$scope.modifyModal;
                    $scope.orderParams.reload();
                }else{
                    toastr.error("修改失败");
                }

            }).error(function(data) {
            });
        };

        //资方列表
        $scope.companyList=function () {
            $http({
                method: 'POST',
                url: "/admin/assetCompany/getList",
            }).success(function(data) {
                $scope.assetList=data;
            }).error(function(data) {
            });
        }
        $scope.companyList();//资方列表


    }]);


});